//----------------------------------------------------------- // Purpose: Implementation of postfix calculator // Author: John Gauch //----------------------------------------------------------- #include "stack5/stack5.h" #include #include using namespace std; //----------------------------------------------------------- // Use stack to evaluate postfix expressions //----------------------------------------------------------- int evaluate() { Stack stack; string str; double num1, num2, res; cout << "Enter postfix expression: "; cin >> str; while (str != "=") { // Push number on stack if ((str[0] >= '0') && (str[0] <= '9')) { stack.Push(atof(str.c_str())); } // Handle addition else if (str == "+") { if (stack.GetLength() < 2) { cout << "Error: Not enough data for '" << str << "' operation\n"; return -1; } stack.Pop(num2); stack.Pop(num1); stack.Push(num1 + num2); } // Handle subtraction else if (str == "-") { if (stack.GetLength() < 2) { cout << "Error: Not enough data for '" << str << "' operation\n"; return -1; } stack.Pop(num2); stack.Pop(num1); stack.Push(num1 - num2); } // Handle multiplication else if (str == "*") { if (stack.GetLength() < 2) { cout << "Error: Not enough data for '" << str << "' operation\n"; return -1; } stack.Pop(num2); stack.Pop(num1); stack.Push(num1 * num2); } // Handle division else if (str == "/") { if (stack.GetLength() < 2) { cout << "Error: Not enough data for '" << str << "' operation\n"; return -1; } stack.Pop(num2); stack.Pop(num1); stack.Push(num1 / num2); } // Handle power else if (str == "^") { if (stack.GetLength() < 2) { cout << "Error: Not enough data for '" << str << "' operation\n"; return -1; } stack.Pop(num2); stack.Pop(num1); stack.Push(pow(num1, num2)); } // Handle absolute value else if (str == "abs") { if (stack.GetLength() < 1) { cout << "Error: Not enough data for '" << str << "' operation\n"; return -1; } stack.Pop(num1); stack.Push(fabs(num1)); } // Handle square root else if (str == "sqrt") { if (stack.GetLength() < 1) { cout << "Error: Not enough data for '" << str << "' operation\n"; return -1; } stack.Pop(num1); stack.Push(sqrt(num1)); } // Handle exp operation else if (str == "exp") { if (stack.GetLength() < 1) { cout << "Error: Not enough data for '" << str << "' operation\n"; return -1; } stack.Pop(num1); stack.Push(exp(num1)); } // Handle log operation else if (str == "log") { if (stack.GetLength() < 1) { cout << "Error: Not enough data for '" << str << "' operation\n"; return -1; } stack.Pop(num1); stack.Push(log(num1)); } // Handle unexpected operation else { cout << "Error: Unexpected '" << str << "' in input\n"; return -1; } cin >> str; } // Error checking on stack if (stack.GetLength() < 1) { cout << "Error: Not enough data for '" << str << "' operation\n"; return -1; } else if (stack.GetLength() > 1) { cout << "Error: Too much data for '" << str << "' operation\n"; return -1; } // Print result stack.Pop(res); cout << "Result = " << res << endl; return 0; } //----------------------------------------------------------- // Main program. //----------------------------------------------------------- int main() { // Loop processing user input while (!cin.eof() && evaluate() != -1); return 0; }